<?php

require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_COST_FLIER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );


Class_PWD::valid_auth(AOKI_AUTH_COST,$login['staff_auth']);
$li_sort = array(
	'0'  => 'すべて(予実比)',
	'1'  => 'チラシ経費',
	'2'  => 'DM経費'
);

$li_unit = array(
	'1'  => '年度単位',
	'2'  => '月度単位',
	'3'  => '週単位'
);

$li_format = array(
	'1'  => '店舗単位',
	'2'  => 'エリア単位',
	'3'  => 'ゾーン単位'
);

$dosearch = FALSE ;
$dao = new Class_M_COST_FLIER;
//ゾーンとエリアの計算開始
//require_once( '../system/cell_ajareks_sort.inc.php' );
//ゾーンとエリアの計算終了

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	 $forms =  $_SESSION['MSEARCH']['COST_FLIER'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['COST_FLIER'] = $forms;
}

$login_auth = $login['staff_auth'];



	//-----------権限 開始---------------------------------
	$area_zone_flg  = 0;
	$login_auth = $login['staff_auth'];
	if ($login_auth == 'G-2' ) {
		$area_cd   = $login['autoid'];
		$forms["sort"] = '2';
		$area_zone_flg = 2;
		$areauser = array($area_cd => $login['staff_id']." ".$login['staff_name'] );
		$forms['area_cd'] =  $area_cd;
	}else if ($login_auth == 'G-3' ) {
		$zone_cd   = $login['autoid'];
		$forms["sort"] = '3';
		$area_zone_flg = 3;
		$cellsort = array();
		$zoneuser = array($zone_cd => $login['staff_id']." ".$login['staff_name'] );
		$forms['zone_cd'] = $zone_cd;
	}else {
		//PLEASE_SELECT_ARR ;
		$zoneuser  = array();
		$zoneuser  = Class_M_OTHER::get_ajaeks_array(NULL, 3);
		$areauser = array();
		$areauser =  Class_M_OTHER::get_ajaeks_array(NULL, 2);
	}
	
	//-----------権限 完了---------------------------------	
	
$aja = "";
if  (($forms["sort"] == '2') &&  $forms["area_cd"]) {
	$aja =  $forms['area_cd'] ;
}

if  (($forms["sort"] == '3') &&  $forms["zone_cd"]) {
	$aja =  $forms['zone_cd'] ;
}
/*
$wherearr =  array();
$shop_name = NULL;
if ( isset( $forms['shop_name'] ) &&  ( !empty( $forms['shop_name'] ) ) ){
	//$wherearr[] = sprintf(" shop_name like  '%s' ",  "%".$forms['shop_name']."%" );
	$shop_name = $forms['shop_name'];
}

$martrix_cd = "";
if ( isset( $forms['week_cd'] ) &&  ( !empty( $forms['week_cd'] ) ) ){
	//$wherearr[] = sprintf("c.matrix_cd =  '%s' ",  $forms['week_cd'] );
	$martrix_cd   =  $forms['week_cd'];
}

*/
$fmunit   = $forms['li_unit'];
if ($fmunit == '1') {
	$forms['week_cd'] = "";
	$forms['month'] = "";
}
if ($fmunit == '2') {
	$forms['week_cd'] = "";
}
if ($fmunit == '3') {
	$forms['month'] = "";
}
$fmsort   = $forms['li_sort'];
$fmformat = $forms['li_format'];
$year4    = $forms['year4'];
$nowmonth = $forms['month'];
$matrix_cd = $forms['week_cd'];


//---CSVダウンロード　処理開始

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "keihi_list".date("Ymd_His");
	header("Content-Type: application/octet-stream");
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");

	
	$colums = array();
//$fmsort
//$fmformat = 1 店舗単位 2 AJAREKS単位 3損  4 月単位 5 週単位
	$csvzonearea = "";
	if ($fmformat == 2) {
		$csvzonearea = "エリア";
	}
	if ($fmformat == 3) {
		$csvzonearea = "ゾーン";
	}

	if ($fmformat == 1) {
		$col_heads1 = array(
			'shop_cd'          => array( '店舗コード',  1),
			'shop_name'        => array( '店名',  1),
			'area_aja'         => array( 'エリアAJA名',  1),
			'zone_aja'         => array( 'ゾーンAJA名',  1)
		);
	}else{
		$col_heads1 = array(
			'aja_cd'           => array( 'AJAREKSコード',  1),
			'aja_name'         => array( 'AJREKKS名',  1),
			'aja_sort'         => array( 'AJA種類',  1)
		);
	}
	if ($fmsort ==0) {	
		$col_heads2 = array(
			'now_qua'        => array( '経準高 本年',  1),
			'plane_qua'      => array( '経準高 計画',  1),
			'last_qua'       => array( '経準高 前年',  1),
			'plane_rate_qua' => array( '経準高 計画費',  1),
			'plane_inc_qua'  => array( '経準高 計画差異',  1),
			'last_rate_qua'  => array( '経準高 前年比',  1),
			'last_inc_qua'   => array( '経準高 前年差異',  1),
			'now_cost'       => array( '経費 本年',  1),
			'plane_cost'     => array( '経費 計画',  1),
			'last_cost'      => array( '経費 前年',  1),
			'plane_rate_cost'=> array( '経費 計画費',  1),
			'plane_inc_cost' => array( '経費 計画差異',  1),
			'last_rate_cost' => array( '経費 前年比',  1),
			'last_inc_cost'  => array( '経費 前年差異',  1),
			'now_li'         => array( '利益 本年',  1),
			'plane_li'       => array( '利益 計画',  1),
			'last_li'        => array( '利益 前年',  1),
			'plane_rate_li'  => array( '利益 計画費',  1),
			'plane_inc_li'   => array( '利益 計画差異',  1),
			'last_rate_li'   => array( '利益 前年比',  1),
			'last_inc_li'    => array( '利益 前年差異',  1)
		);
	}else {
		$col_heads2 = array(
			'now_cost'       => array( '経費 本年',  1),
			'plane_cost'     => array( '経費 計画',  1),
			'last_cost'      => array( '経費 前年',  1),
			'plane_rate_cost'=> array( '経費 計画費',  1),
			'plane_inc_cost' => array( '経費 計画差異',  1),
			'last_rate_cost' => array( '経費 前年比',  1),
			'last_inc_cost'  => array( '経費 前年差異',  1)

		);
	}
	
	$col_heads = array_merge($col_heads1, $col_heads2);
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	$page  = null;
	$data = $dao->searchList($page , $fmsort ,$fminit,$fmformat ,$year4, $matrix_cd, $forms["month"] , $aja );
		
	foreach ($data as $k => $v ) {
		
		$data[$k]["plane_cost"]  = $v["plane_cost"] + $v["plane_dm"];
		$data[$k]["now_cost"]    = $v["now_cost"]   + $v["nowe_dm"];
		$data[$k]["last_cost"]   = $v["last_cost"]  + $v["last_dm"];
		
		
		if ($fmsort =='0' ){
			$data[$k]["plane_li"]  = $v["plane_qua"] - $data[$k]["plane_cost"];
			$data[$k]["now_li"]    = $v["now_qua"]   - $data[$k]["now_cost"];
			$data[$k]["last_li"]   = $v["last_qua"]  - $data[$k]["last_cost"];
			
			
			
		}
		
		Class_PWD::set_rate_inc_by_nm(&$data, $k, "cost");
		if ($fmsort =='0' ){			
			Class_PWD::set_rate_inc_by_nm(&$data, $k, "qua");
			Class_PWD::set_rate_inc_by_nm(&$data, $k, "li");
		}
	}
	
	//$csvzonearea
	if (($fmformat == 2) or ($fmformat == 3)) {
		foreach ($data as $k => $v ) {
			$data[$k]["aja_sort"]  = $csvzonearea ;
		}
	}
	
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

	if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
		$dosearch = TRUE;
	}

	if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
		$dosearch = TRUE;
		$page['current'] = $forms['page'];
	}else{
		$page['current'] = 1;
	}

	$page['item'] = PAGE_MAX;
	if ($dosearch) {
		//$matr = Class_M_OTHER::get_by_matrix_cd( $forms['week_cd']);
		
		//$page内容は関数内部更新されます。
		//data = $dao->search( $wherearr, $page, $forms['week_cd'], $matr["pre_matrix_cd"] );
		$data = $dao->searchList($page , $fmsort ,$fminit,$fmformat ,$year4, $matrix_cd, $forms["month"] , $aja );
		
		//表示ために整形開始
		foreach ($data as $k => $v ) {
			
			$data[$k]["plane_cost"]  = $v["plane_cost"] + $v["plane_dm"];
			$data[$k]["now_cost"]    = $v["now_cost"]   + $v["nowe_dm"];
			$data[$k]["last_cost"]   = $v["last_cost"]  + $v["last_dm"];

			if ($fmsort =='0' ){
				$data[$k]["plane_li"]  = $v["plane_qua"] - $data[$k]["plane_cost"];
				$data[$k]["now_li"]    = $v["now_qua"]   - $data[$k]["now_cost"];
				$data[$k]["last_li"]   = $v["last_qua"]  - $data[$k]["last_cost"];
			}
			
			Class_PWD::set_rate_inc_by_nm(&$data, $k, "cost");
			if ($fmsort =='0' ){			
				Class_PWD::set_rate_inc_by_nm(&$data, $k, "qua");
				Class_PWD::set_rate_inc_by_nm(&$data, $k, "li");
			}
			//$data[$k] = $v;
		}
	}

//表示ために整形完了
if (empty($forms["year4"])) {
	//$forms["year4"] = date('Y');
	$forms["year4"] = date('Y');
}

if (empty($forms["week_cd"])) {
	$forms["week_cd"] = "";
}

if (empty($forms["month"])){
	$forms["month"] = 1;
}

$smarty->assign('year4',  Class_PWD::get_years());
$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
$smarty->assign('week', $week );
//test


//週名を設定する
foreach ($week as $k => $v) {
	if ($k == $martrix_cd){
		$smarty->assign('weekname', $v );
		break;
	}
}
$month = array();
for ($i = 1; $i <= 12; $i++) {
    $month[$i] =  $i ;
}
$smarty->assign('month', $month );

//ゾーンとエリア名称を設定する
$aoki_area_zones = array();
$aoki_area_zone=array_reverse($aoki_area_zone,true);
foreach ($aoki_area_zone as $k => $v) {
	$aoki_area_zones[$k] = $v;
}
$smarty->assign('sorts', $aoki_area_zones );

//出力形式
/*
$outputlayoutstmp = array();
foreach ($outputlayout as $k => $v) {
	$outputlayoutstmp[$k] = $v;
}
$forms["outputlayout"]='0';
$smarty->assign('outputlayouts', $outputlayoutstmp );
*/
//================AJAゾーンエリア==================
if( isset($zoneuser) )    $smarty->assign('zoneuser', $zoneuser);
if( isset($areauser) )    $smarty->assign('areauser', $areauser);
//================AJAゾーンエリア==================
if ( empty( $forms["sort"] ) ) {
	$forms["sort"] = "";
}

$aoki_area_zones = array();
if (($login_auth == 'G-2' ) || ($login_auth == 'G-3' ))  {
	foreach ($aoki_area_zone as $k => $v) {
		if ($k == $forms["sort"] ) {
			$aoki_area_zones[$k] = $v;
		} 
	}
}else {
	$aoki_area_zones[""] = "すべて";
	foreach ($aoki_area_zone as $k => $v) {
		$aoki_area_zones[$k] = $v;
	}
}
$smarty->assign('sorts', $aoki_area_zones );
//==================================周
$smarty->assign('li_sort', $li_sort);
$smarty->assign('li_unit', $li_unit);
$smarty->assign('li_format', $li_format);

if (empty($forms["li_sort"])){
	$forms["li_sort"] = 0 ;
}

if (empty($forms["li_unit"])){
	$forms["li_unit"] = 3 ;
}

if (empty($forms["li_format"])){
	$forms["li_format"] = 1 ;
}



if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_list_index.html');
?>